package com.learning.rpc_service.server;

import com.learning.rpc_common.request.RpcRequest;
import com.learning.rpc_common.request.RpcResponse;
import com.learning.rpc_common.serializer.JSONSerializer;
import com.learning.rpc_common.serializer.RpcDecoder;
import com.learning.rpc_common.serializer.RpcEncoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.http.HttpObjectAggregator;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class NettyService {
    public static void initService() throws InterruptedException {
        // 1.创建NioEventLoopGroup的两个实例对象
        // 它们就是两个线程池 ，默认的线程数就是CPU核心数*2
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        //2.创建服务启动辅助类:装配一些组件的
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup,workerGroup)
                //指定服务器端监听套接字通道NioServerSocketChannel
                .channel(NioServerSocketChannel.class)
                //设置业务职责链 channelhandle组成
                //ChannelInitializer:初始化channel的chanelpipeline
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        //将一个一个的channelhandler添加到责任链上，在请求进来或者响应出去的时候，都会经过链上channelhandler进行处理
//                        pipeline.addLast(new StringEncoder());
//                        pipeline.addLast(new StringDecoder());
                        // 自定义长度解码器解决TCP黏包问题
                        // maxFrameLength 最大包字节大小，超出报异常
                        // lengthFieldOffset 长度字段的偏差
                        // lengthFieldLength 长度字段占的字节数
                        // lengthAdjustment 添加到长度字段的补偿值
                        // initialBytesToStrip 从解码帧中第一次去除的字节数
                        pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE
                                , 0, 2, 0, 2));
                        pipeline.addLast(new HttpObjectAggregator(Integer.MAX_VALUE));
                        // LengthFieldPrepender编码器，它可以计算当前待发送消息的二进制字节长度，将该长度添加到ByteBuf的缓冲区头中
//                        pipeline.addLast(new LengthFieldPrepender(2));
                        pipeline.addLast(new RpcEncoder(RpcResponse.class, new JSONSerializer()));
                        pipeline.addLast(new RpcDecoder(RpcRequest.class, new JSONSerializer()));
                        pipeline.addLast(new ServerHandler());
                    }
                });

        // bind 监听端口
        //sync:用于阻塞当前的thread,一直到端口绑定操作完成
        ChannelFuture f = bootstrap.bind(8088).sync();
        System.out.println("tcp sevice start success..");
        //应用程序会阻塞等待直到服务器的channel关闭
//        f.channel().closeFuture().sync();
    }
}
